package com.atguigu.guli.service.edu.controller.admin;


import com.atguigu.guli.service.base.result.R;
import com.atguigu.guli.service.edu.entity.Teacher;
import com.atguigu.guli.service.edu.entity.TeacherQuery;
import com.atguigu.guli.service.edu.service.TeacherService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author ZMR
 * @since 2020-12-15
 */
@Api(tags = "讲师管理")
@RestController
@RequestMapping("/admin/edu/teacher")
@Slf4j
@CrossOrigin
public class AdminTeacherController {
    @Autowired
    TeacherService teacherService;

    @ApiOperation("所有讲师列表")
    @GetMapping("list")
    public R listAll() {
        List<Teacher> list = teacherService.list();
        return R.ok().data("items", list).message("获取讲师列表成功");
    }

    @ApiOperation("根据id删除讲师")
    @DeleteMapping("remove/{id}")
    public R removeById(@ApiParam(value = "讲师id", required = true) @PathVariable String id) {
        boolean remove = teacherService.removeById(id);
        if (remove) {
            return R.ok().message("删除成功");
        } else {
            return R.error().message("数据不存在");
        }
    }


    @ApiOperation("带条件的分页查询")
    @GetMapping("list/{pageNumber}/{pageSize}")
    public R listPage(@ApiParam(value = "当前页码", required = true) @PathVariable Integer pageNumber,
                      @ApiParam(value = "每页记录数", required = true) @PathVariable Integer pageSize, TeacherQuery teacherQuery) {

        Page<Teacher> teacherPage = new Page<>(pageNumber, pageSize);
        Page<Teacher> page = teacherService.selectTeacherPageByCondition(pageNumber, pageSize, teacherQuery);
        return R.ok().data("pageModel", page);
    }

    @ApiOperation("新增讲师")
    @PostMapping("save")
    public R save(@ApiParam(value = "讲师对象", required = true) @RequestBody Teacher teacher) {
//        设置默认值

        teacher.setJoinDate(new Date());
        System.out.println(teacher);

        if (StringUtils.isEmpty(teacher.getAvatar())) {
            teacher.setAvatar("www.online-education-findOut");
        }
        boolean save = teacherService.save(teacher);
        if (save) {
            return R.ok().message("添加成功");
        } else {
            return R.error().message("添加失败");
        }
    }

    @ApiOperation("根据id修改")
    @PutMapping("update")
    public R updateById(@ApiParam(value = "讲师对象", required = true) @RequestBody Teacher teacher) {
        boolean result = teacherService.updateById(teacher);
        if (result) {
            return R.ok().message("更新成功");
        } else {
            return R.ok().message("数据不存在");
        }
    }

    @ApiOperation("根据id查询")
    @GetMapping("get/{id}")
    public R getById(@ApiParam(value = "讲师ID", required = true) @PathVariable String id) {
        Teacher teacher = teacherService.getById(id);
        if (teacher != null) {
            return R.ok().data("items", teacher);
        } else {
            return R.error().message("查询失败");
        }
    }
}

